package cn.duxiaod.demo.design.test.agent;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.FactoryBean;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

public class MapperFactoryBean<T> implements FactoryBean<T> {
    private Logger logger = LoggerFactory.getLogger(MapperFactoryBean.class);
    private Class<T> mapperClass;
    public MapperFactoryBean(Class<T> mapperClass){
        this.mapperClass = mapperClass;
    }
    @Override
    public T getObject() throws Exception {
        InvocationHandler handler = (proxy,method,args)->{
            Select annotation = method.getAnnotation(Select.class);
            logger.info("SQL:{}",annotation.value().replace("#{uid}",args[0].toString()));
            return args[0]+",返回用户信息！";
        };
        return (T)Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[]{mapperClass},handler);
    }

    @Override
    public Class<?> getObjectType() {
        return mapperClass;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}
